package com.atguigu.web;

import com.atguigu.pojo.Book;
import com.atguigu.pojo.Cart;
import com.atguigu.pojo.CartItem;
import com.atguigu.service.BookService;
import com.atguigu.service.impl.BookServiceImpl;
import com.atguigu.utils.WebUtils;
import com.google.gson.Gson;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@WebServlet(name = "CartServlet", value = "/CartServlet")
public class CartServlet extends BaseServlet {
private BookService bookService = new BookServiceImpl();

    protected void addItem(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int id = WebUtils.parseInt(request.getParameter("id"),0);
        Book book = bookService.queryBookById(id);
        CartItem cartItem = new CartItem(book.getId(), book.getName(),1,book.getPrice(),book.getPrice());
        Cart cart = (Cart) request.getSession().getAttribute("cart");
        if (cart==null){
            cart = new Cart();
            request.getSession().setAttribute("cart",cart);
        }
        cart.addItem(cartItem);

        System.out.println(cart);

        System.out.println("请求头 Referer 的值：" + request.getHeader("Referer"));
        request.getSession().getAttribute("cartItem");
        request.getSession().setAttribute("lastname",cartItem.getName());
        response.sendRedirect(request.getHeader("Referer"));
    }

    protected void deleteItem(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取要删除的商品id
        int id = WebUtils.parseInt(request.getParameter("id"),0);
//        获取购物车对象
Cart cart = (Cart) request.getSession().getAttribute("cart");
        System.out.println(cart);
if (cart!=null){
    cart.deleteItem(id);
    response.sendRedirect(request.getHeader("Referer"));
}

    }

    protected void clearItem(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    Cart cart = (Cart) request.getSession().getAttribute("cart");
    if (cart != null){
        cart.clear();
        response.sendRedirect(request.getHeader("Referer"));
    }
    }

    protected void updateCount(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    int id = WebUtils.parseInt(request.getParameter("id"),0);
    int count = WebUtils.parseInt(request.getParameter("count"),1);
    Cart cart = (Cart) request.getSession().getAttribute("cart");
    if (cart != null){
        cart.updateCount(id,count);
        response.sendRedirect(request.getHeader("Referer"));
    }
    }

    protected void AjaxAddItem(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int id = WebUtils.parseInt(request.getParameter("id"),0);
        Book book = bookService.queryBookById(id);
        CartItem cartItem = new CartItem(book.getId(), book.getName(),1,book.getPrice(),book.getPrice());
        Cart cart = (Cart) request.getSession().getAttribute("cart");
        if (cart==null){
            cart = new Cart();
            request.getSession().setAttribute("cart",cart);
        }
        cart.addItem(cartItem);

        System.out.println(cart);
//        request.getSession().getAttribute("cartItem");
        request.getSession().setAttribute("lastname",cartItem.getName());

        Map<String,Object> resultMap = new HashMap<String,Object>();
        resultMap.put("totalCount",cart.getTotalCount());
        resultMap.put("lastname",cartItem.getName());
        Gson gson = new Gson();
      String  strResultMap =  gson.toJson(resultMap);
        response.getWriter().write(strResultMap);
    }
    }
